<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录页</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <style>
        body {
            background-color: #f2f2f2;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-image: url("../layui/img/userloginback2.jpg");
            background-size: cover;
        }
        .login-container {
            margin: 0 auto;
            /*width: 400px;*/
            width: 50%;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            background-color: rgba(0, 0, 0, 0); /* 使用rgba()函数设置背景颜色和透明度 */
        }
        .login-container h2 {
            text-align: center;
            font-size: 24px;
            margin-bottom: 20px;
        }
        .login-container .layui-form-item {
            margin-bottom: 20px;
        }
        .login-container .layui-input-block {
            width: 100%;
        }
        .login-container .layui-input-block input {
            width: 100%;
            font-size: 14px;
        }
        .login-container .layui-btn-container {
            text-align: center;
        }
        .login-container .layui-btn-container .layui-btn {
            width: 120px;
        }
        .login-container .layui-btn-container .layui-btn:first-child {
            margin-right: 20px;
        }

        /**加深登录界面字体*/
        .bold-label {
            font-weight: bold;
        }
    </style>
</head>
<script src="../layui/layui.js"></script>
<body>
<div class="layui-container">
    <div class="login-container">
        <form class="layui-form" id="login-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label bold-label" style="color: red;">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input" style="width: 60%;">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label bold-label" style="color: red;">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input" style="width: 60%;">
                </div>
            </div>
            <div class="layui-btn-container">
                <button class="layui-btn bold-label" lay-submit lay-filter="login">登录</button>
                <button class="layui-btn layui-btn-primary bold-label" onclick="location.href='/kamun/user/register'" type="button">注册</button>
            </div>
        </form>
    </div>
</div>

<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        // 监听表单提交事件
        form.on('submit(login)', function (data) {
            // 阻止表单提交
            data.preventDefault();

            // 获取表单数据
            var username = data.field.username;
            var password = data.field.password;

            // 构建请求数据
            var requestData = {
                username: username,
                password: password
            };

            // 发送登录请求
            $.ajax({
                url: '/user/login', // 后端登录接口的URL，请根据实际情况进行修改
                type: 'POST',
                data: requestData,
                success: function (response) {
                    // 登录成功
                    if (response.success) {
                        layer.msg('登录成功', { icon: 1 });
                        window.location.href = '/kamun/product/findAll'; // 登录成功后跳转的页面，请根据实际情况进行修改
                    } else {
                        window.alert("用户名或密码错误");
                        layer.msg('用户名或密码错误', { icon: 2 });
                    }
                },
                error: function (xhr, status, error) {
                    // 处理错误情况
                    if (status === 'timeout') {
                        layer.alert('登录状态失效，请重新登录', {
                            title: '提示',
                            closeBtn: 0
                        }, function () {
                            window.location.href = '/kamun/user/login';
                        });
                    } else {
                        layer.msg('登录请求失败', { icon: 2 });
                    }
                }
            });

            return false; // 阻止表单跳转
        });

    });
</script>
</body>
</html>
